package com.tbit.uqbike.client.service.impl;

import com.tbit.uqbike.client.dao.core.InviteNewEventDao;
import com.tbit.uqbike.client.dao.log.EvenJoinDetailDao;
import com.tbit.uqbike.client.pojo.EvenJoinDetail;
import com.tbit.uqbike.client.pojo.InviteNewEvent;
import com.tbit.uqbike.client.pojo.User;
import com.tbit.uqbike.client.service.EvenJoinDetailService;
import com.tbit.uqbike.client.util.IdCardUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: chen
 * @Description:
 * @Date: 创建于 19:19 2022/5/11
 */
@Service
public class EvenJoinDetailServiceImpl implements EvenJoinDetailService {

    @Autowired
    private EvenJoinDetailDao evenJoinDetailDao;
    @Autowired
    private InviteNewEventDao inviteNewEventDao;

    @Override
    public EvenJoinDetail getByTypeAndEventId(Integer eventId, Integer type) {
        Map<String, Object> map = new HashMap<>();
        map.put("eventId", eventId);
        map.put("type", type);
        return evenJoinDetailDao.getByTypeAndEventId(map);
    }

    @Override
    public void add(Integer eventId, Integer type, User user, int userCount) {
        InviteNewEvent inviteNewEvent = inviteNewEventDao.getByEventId(eventId);
        if (inviteNewEvent == null) {
            return;
        }
        EvenJoinDetail evenJoinDetail = new EvenJoinDetail();
        evenJoinDetail.setEventId(eventId);
        evenJoinDetail.setUserCount(userCount);
        evenJoinDetail.setType(type);
        evenJoinDetail.setEventType(inviteNewEvent.getType());
        evenJoinDetail.setAccountId(user.getAccountId());
        evenJoinDetail.setAdAccountId(inviteNewEvent.getAccountId());
        if (user.getIdNO() != null) {
            /**计算性别分布*/
            Integer sex = IdCardUtils.getSexFromIdCard(user.getIdNO());
            if (sex.equals(2)) {
                evenJoinDetail.setWoman(1);
            } else if (sex.equals(1)) {
                evenJoinDetail.setMan(1);
            }
            /**计算年龄分布*/
            int age = IdCardUtils.getAgeForIdcard(user.getIdNO());
            if (age >= 16 && age <= 24) {
                evenJoinDetail.setJuvenile(1);
            } else if (age >= 25 && age <= 29) {
                evenJoinDetail.setTeenager(1);
            } else if (age >= 30 && age <= 39) {
                evenJoinDetail.setYoungMan(1);
            } else if (age >= 40 && age <= 49) {
                evenJoinDetail.setMiddleAged(1);
            } else if (age >= 50) {
                evenJoinDetail.setElderly(1);
            }
        }
        EvenJoinDetail evenJoinDetailDB = getByTypeAndEventId(eventId, type);
        if (evenJoinDetailDB == null) {
            evenJoinDetailDao.add(evenJoinDetail);
        } else {
            evenJoinDetailDao.edit(evenJoinDetail);
        }


    }


}
